#!/bin/bash
#========================================================================
# Author: YSLin
# Email: 
# File Name: uudecode.sh
# Description: 
#  syntax:
#   getopts option-string variable
#
# Edit History: 
#   2010-12-23    File created.
#========================================================================


#encode binary files (executable files) into ASCII text so that they could be emailed or transferred via FTP. 
#Today, MIME encoding has taken the place of uuencoding for email attachments, 
#but it is still used for posting binaries to newsgroups and transferring them via modem.
#-f to indicate the input filename
# -o to indicate the output filename
# -v to indicate the script should be verbose


USAGE="Usage: `basename $0` [-v] [-f] [filename] [-o] [filename]";
VERBOSE=false
while getopts f:o:v OPTION ; do
	case "$OPTION" in
		f) 
			INFILE="$OPTARG" ;;
		o) 
			OUTFILE="$OPTARG" ;;
		v) 
			VERBOSE=true ;;
		\?) echo "$USAGE" ;
			exit 1
			;;
	esac
done
#實際傳進的參數量
shift `echo "$OPTIND - 1" | bc` 
echo "$(($OPTIND - 1))"
if [ -z "$1" -a -z "$INFILE" ] ; then
	echo "ERROR: Input file was not specified."
	exit 1
fi
if [ -z "$INFILE" ] ; then INFILE="$1" ; fi
: ${OUTFILE:=${INFILE}.uu}
if [ -f "$INFILE" ] ; then
	if [ "$VERBOSE" = "true" ] ; then
		echo "uuencoding $INFILE to $OUTFILE... \c"
	fi
	uuencode $INFILE $INFILE > $OUTFILE ; RET=$?
	if [ "$VERBOSE" = "true" ] ; then
		MSG="Failed" ; if [ $RET -eq 0 ] ; then MSG="Done." ; fi
		echo $MSG
	fi
fi
exit 0
